trigger AASCampaignFlightTrigger on Opportunity (after insert, after update) 
{
List<Event> lstInsertEvent=new List<Event>();
List<Event> lstUpdateEvent=new List<Event>();
Map<Id,List<Event>> MapEvent=new Map<Id,List<Event>>();
//Recordtype rectype=[select id from RecordType where name='CAMPAIGN FLIGHT' and SobjectType='Event'];
ID publicCalId=AppEndPoints__c.getInstance().Campaign_Flight__c;
Set<ID> oppAASRecordTypeID = AASFieldUpdateHelperClass.getOpportunityAASRecordTypeID();
if(trigger.isInsert)
{
    //change by naidu
   Schema.DescribeSObjectResult d = Schema.SObjectType.Event;
    Id rectype = d.getRecordTypeInfosByName().get('CAMPAIGN FLIGHT').getRecordTypeId();
//end of change
    for(Opportunity opp:trigger.new)
    {        
        if(opp.Probability >=75 && oppAASRecordTypeID.contains(opp.RecordTypeId) && opp.Contract_Start_Date__c!=null && opp.Contract_Termination_Date__c!=null)
        {           
            
            Date stdate=opp.Contract_Start_Date__c;
            Date enddate=opp.Contract_Termination_Date__c;
            integer numberDaysDue =stdate.daysBetween(enddate);
           /* if(numberDaysDue>14)
            {
            opp.addError('Campaign Start Date and End date difference should not be more than 14 for Calender creation');
            break;
            }*/
            String oppname=opp.Name;
            string subject=string.valueof(stdate.month())+string.valueof(stdate.month())+string.valueof(stdate.day())+string.valueof(stdate.year())+'-'+
            string.valueof(enddate.month())+string.valueof(enddate.day())+string.valueof(enddate.year())+' : '+
            oppname;
            Event evnt1=new Event(Type='CAMPAIGN FLIGHT',StartDateTime=opp.Contract_Start_Date__c,EndDateTime=opp.Contract_Start_Date__c,
            RecordTypeId=rectype,Subject=subject,IsAllDayEvent=True,IsReminderSet=False,WhatId=opp.id,OwnerId=publicCalId);
            Event evnt2=new Event(Type='CAMPAIGN FLIGHT',StartDateTime=opp.Contract_Termination_Date__c,EndDateTime=opp.Contract_Termination_Date__c,
            RecordTypeId=rectype,Subject=subject,IsAllDayEvent=True,IsReminderSet=False,WhatId=opp.id,OwnerId=publicCalId);
            lstInsertEvent.add(evnt1);
            lstInsertEvent.add(evnt2);
        }       
    }
}

if(trigger.isUpdate)
{
    //change by naidu
   Schema.DescribeSObjectResult d = Schema.SObjectType.Event;
    Id rectype = d.getRecordTypeInfosByName().get('CAMPAIGN FLIGHT').getRecordTypeId();
//end of change
    List<Event> lstEvent=[Select id,StartDateTime,EndDateTime,WhatId from Event where Whatid in:trigger.new and OwnerId=:publicCalId];
    string whatid;
    List<Event> SetEvent=new List<Event>();
    for(Event evnt:lstEvent)
    {
    SetEvent.add(evnt);
    whatid=evnt.WhatId;
    }
    MapEvent.put(whatid,SetEvent);
    for(Opportunity opp:trigger.new)
    {
        system.debug('testProbability'+opp.Probability);
        if(opp.Probability>=75 && oppAASRecordTypeID.contains(opp.RecordTypeId) && opp.Contract_Start_Date__c!=null && opp.Contract_Termination_Date__c!=null)
        {
            List<Event> evntupdate=MapEvent.get(opp.Id);
            if(evntupdate!=null)
            {
                 system.debug('testProbability1'+opp.Probability);
                if(trigger.oldmap.get(opp.id).Contract_Start_Date__c!=opp.Contract_Start_Date__c || trigger.oldmap.get(opp.id).Contract_Termination_Date__c!=opp.Contract_Termination_Date__c)
                {       
                    Date stdate=opp.Contract_Start_Date__c;
                    Date enddate=opp.Contract_Termination_Date__c;
                    integer numberDaysDue =stdate.daysBetween(enddate);
                    /*if(numberDaysDue>14)
                    {
                    opp.addError('Campaign Start Date and End date difference should not be more than 14 for Calender creation');
                    break;
                    }*/
                    String oppname=opp.Name;
                    string subject=string.valueof(stdate.month())+string.valueof(stdate.month())+string.valueof(stdate.day())+string.valueof(stdate.year())+'-'+
                    string.valueof(enddate.month())+string.valueof(enddate.day())+string.valueof(enddate.year())+' : '+
                    oppname;
                    Event evnts1=evntupdate[0];                 
                    evnts1.StartDateTime=opp.Contract_Start_Date__c;
                    evnts1.Subject=subject;
                    evnts1.RecordTypeId=rectype;
                    evnts1.EndDateTime=opp.Contract_Start_Date__c;
                    lstUpdateEvent.add(evnts1);
                    Event evnts2=evntupdate[1];                 
                    evnts2.StartDateTime=opp.Contract_Termination_Date__c;
                    evnts2.Subject=subject;
                    evnts2.RecordTypeId=rectype;
                    evnts2.EndDateTime=opp.Contract_Termination_Date__c;
                    lstUpdateEvent.add(evnts2);
                    
                
                }
            }
            else
            {
                system.debug('testProbability2'+opp.Probability);
                Date stdate=opp.Contract_Start_Date__c;
                Date enddate=opp.Contract_Termination_Date__c;
                integer numberDaysDue =stdate.daysBetween(enddate);
               /* if(numberDaysDue>14)
                {
                opp.addError('Campaign Start Date and End date difference should not be more than 14 for Calender creation');
                break;
                }*/
                String oppname=opp.Name;
                string subject=string.valueof(stdate.month())+string.valueof(stdate.month())+string.valueof(stdate.day())+string.valueof(stdate.year())+'-'+
                string.valueof(enddate.month())+string.valueof(enddate.day())+string.valueof(enddate.year())+' : '+
                oppname;
                Event evnt1=new Event(Type='CAMPAIGN FLIGHT',StartDateTime=opp.Contract_Start_Date__c,EndDateTime=opp.Contract_Start_Date__c,
                RecordTypeId=rectype,Subject=subject,IsAllDayEvent=True,IsReminderSet=False,WhatId=opp.id,OwnerId=publicCalId);
                Event evnt2=new Event(Type='CAMPAIGN FLIGHT',StartDateTime=opp.Contract_Termination_Date__c,EndDateTime=opp.Contract_Termination_Date__c,
                RecordTypeId=rectype,Subject=subject,IsAllDayEvent=True,IsReminderSet=False,WhatId=opp.id,OwnerId=publicCalId);
                lstInsertEvent.add(evnt1);
                lstInsertEvent.add(evnt2);
            }
        }
        else if(opp.Probability<75)
        {
            if(SetEvent.size()>0)
            delete SetEvent;
        }
    }
}
system.debug('testlstInsertEvent'+lstInsertEvent);
if(lstInsertEvent.size()>0)
insert lstInsertEvent;
system.debug('testlstInsertEvent'+lstInsertEvent);
system.debug('testlstUpdateEvent'+lstUpdateEvent);
if(lstUpdateEvent.size()>0)
update lstUpdateEvent;
system.debug('testlstUpdateEvent'+lstUpdateEvent);
}